﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model.Rules;
using System.Xml;

namespace iLangl.Model
{
	public class ProjectRequestContext : BaseContext
	{
		protected Project _project = null;
		public override void SetContext(object context)
		{
			base.SetContext(context);

			if (context is Project)
			{
				_project = context as Project;
			}
		}


		public override object GetContext()
		{
			if (_project == null)
				return string.Empty;



			startDocument();
			Writer(writer);
			return endDocument();

		}




		private void writeContents(XmlWriter writer)
		{
			writer.WriteStartElement("Content");
			

			foreach (ContentEntity ce in _project.Contents)
			{
				writer.WriteStartElement("ContentEntity");
				
				writer.WriteAttributeString("id", ce.Id);
				writer.WriteAttributeString("name", ce.Name);
				writer.WriteAttributeString("version", ce.Version);
				writer.WriteAttributeString("type", ce.Type);
				writer.WriteAttributeString("language", ce.Language.LanguageId);
				InitExtendedAttributes(writer, ce);
				writer.WriteStartElement("Body");
				writer.WriteCData(ce.Content);
				writer.WriteEndElement();
				writer.WriteEndElement();
			}
			writer.WriteEndElement();
		}

		private void writeTranslations(XmlWriter writer, ContactRequestContext contactcontext)
		{
			writer.WriteStartElement("Translations");
			
			foreach (Translation trans in _project.Translations)
			{
				writer.WriteStartElement("Translation");
				
				writer.WriteAttributeString("id", trans.Id);
				writer.WriteAttributeString("progress", trans.Progress.ToString());
				writer.WriteAttributeString("status", trans.Status);
				writer.WriteAttributeString("comment", trans.Comment);
				
				if (trans.SourceContent != null)
				{
					writer.WriteAttributeString("sourceContentId", trans.SourceContent.Id);
				}

				if (trans.TargetContent != null)
				{
					writer.WriteAttributeString("targetContentId", trans.TargetContent.Id);
				}

				if (trans.LanguagePair != null)
				{
					writer.WriteAttributeString("sourceLanguage", trans.LanguagePair.SourceLanguage.LanguageId);
					writer.WriteAttributeString("targetLanguage", trans.LanguagePair.TargetLanguage.LanguageId);
				}

				InitExtendedAttributes(writer, trans);

				if (trans.Responsible != null)
				{
					writer.WriteStartElement("Responsible");
					contactcontext.SetContext(trans.Responsible);
					writer.WriteString(contactcontext.GetContext().ToString());
					writer.WriteEndElement();
				}

				if (trans.History != null)
				{
					writer.WriteStartElement("Histories");
				

					foreach (StateHistory shist in trans.History)
					{
						writer.WriteStartElement("StateHistory");
						

						writer.WriteAttributeString("state", shist.State);
						writer.WriteAttributeString("date", shist.ChangedDate.ToString(DefaultSettingsManager.DateTimeFormat));
						writer.WriteAttributeString("ownerid", shist.ChangedPerson.Id);
					}
					writer.WriteEndElement();
				}

				writer.WriteEndElement();
			}

			InitExtendedInfo(writer);
			writer.WriteEndElement();
		}

		public void Writer(XmlWriter writer)
		{
			writer.WriteStartElement("Project");


			writer.WriteAttributeString("projId", _project.Id);
			writer.WriteAttributeString("projName", _project.Name);
			writer.WriteAttributeString("projDescriptor", _project.Description);
			writer.WriteAttributeString("projDeadline", _project.Deadline.ToString(DefaultSettingsManager.DateTimeFormat));
			writer.WriteAttributeString("projStatus", _project.Status);


			InitExtendedAttributes(writer, _project);

			InitExtendedElements(writer, _project);

			writer.WriteStartElement("ProjectLanguages");

			foreach (Language lang in _project.GetProjectLanguages(true))
			{
				writer.WriteStartElement("Language");
				writer.WriteAttributeString("langId", lang.LanguageId);
				writer.WriteAttributeString("langName", lang.LanguageName);
				writer.WriteEndElement();
			}

			writer.WriteEndElement();//ProjectLanguages



			if (_project.WorkArea != null)
			{
				writer.WriteStartElement("Terminology");
				writer.WriteAttributeString("id", _project.WorkArea.Id);
				writer.WriteAttributeString("name", _project.WorkArea.Name);
				writer.WriteEndElement();
			}

			if (_project.Product != null)
			{
				writer.WriteStartElement("Product");
				writer.WriteAttributeString("id", _project.Product.Id);
				writer.WriteAttributeString("name", _project.Product.Name);
				writer.WriteEndElement();
			}

			ContactRequestContext contactcontext = new ContactRequestContext();
			if (_project.Owner != null)
			{
				writer.WriteStartElement("Owner");
				contactcontext.SetContext(_project.Owner);
				contactcontext.Write(writer);
				writer.WriteEndElement();
			}



			if (_project.TechnicalContact != null)
			{
				writer.WriteStartElement("TechnicalContact");
				contactcontext.SetContext(_project.TechnicalContact);
				contactcontext.Write(writer);
				writer.WriteEndElement();
			}

			if (_project.History != null)
			{
				writer.WriteStartElement("StateHistories");


				foreach (StateHistory shist in _project.History)
				{
					writer.WriteStartElement("StateHistory");


					writer.WriteAttributeString("state", shist.State);
					writer.WriteAttributeString("date", shist.ChangedDate.ToString(DefaultSettingsManager.DateTimeFormat));
					writer.WriteAttributeString("ownerid", shist.ChangedPerson.Id.ToString());
					writer.WriteEndElement();
				}

				writer.WriteEndElement();
			}

			if (_project.Contents != null)
			{
				writeContents(writer);

			}
			if (_project.Translations != null)
			{
				writeTranslations(writer, contactcontext);

			}

			ValidateProject vproject = new ValidateProject();
			IList<IError> errors = vproject.Validate(_project);

			if ((errors != null) && (errors.Count > 0))
			{

				ErrorsRequestContext errorRequestContext = new ErrorsRequestContext();
				errorRequestContext.SetContext(errors);
				errorRequestContext.Write(writer);
			}

			writer.WriteEndElement();//root
		}
	}
}